package com.ruoyi.store.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.agent.domain.SysAgent;
import com.ruoyi.agent.domain.SysAgentStatusDTO;
import com.ruoyi.agent.mapper.SysAgentMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.store.domain.SysStoreListVO;
import com.ruoyi.store.domain.SysStoreStatusDTO;
import com.ruoyi.store.mapper.SysStoreMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.store.domain.SysStore;
import com.ruoyi.store.service.ISysStoreService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 管理Controller
 *
 * @author ruoyi
 * @date 2024-06-12
 */
@RestController
@RequestMapping("/store/storeManage")
public class SysStoreController extends BaseController
{
    @Autowired
    private ISysStoreService sysStoreService;

    @Autowired
    private SysStoreMapper sysStoreMapper;

    @Autowired
    private SysAgentMapper agentMapper;

    /**
     * 查询管理列表
     */

    @GetMapping("/list")
    public TableDataInfo list(SysStore sysStore)
    {
        startPage();
        List<SysStore> list = sysStoreService.selectSysStoreList(sysStore);
        return getDataTable(list);
    }

    /**
     * 导出管理列表
     */

    @Log(title = "管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysStore sysStore)
    {
        List<SysStore> list = sysStoreService.selectSysStoreList(sysStore);
        ExcelUtil<SysStore> util = new ExcelUtil<SysStore>(SysStore.class);
        util.exportExcel(response, list, "管理数据");
    }

    /**
     * 获取管理详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysStoreService.selectSysStoreById(id));
    }

    /**
     * 新增管理
     */

    @Log(title = "管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysStore sysStore)
    {
        return toAjax(sysStoreService.insertSysStore(sysStore));
    }

    /**
     * 修改管理
     */

    @Log(title = "管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysStore sysStore)
    {
        return toAjax(sysStoreService.updateSysStore(sysStore));
    }

    /**
     * 删除管理
     */

    @Log(title = "管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/deleteIds/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        System.out.println(ids);
        return toAjax(sysStoreService.deleteSysStoreByIds(ids));
    }


    @PostMapping("/listMyStroe")
    public AjaxResult listMyStore() {
        Long deptId = SecurityUtils.getDeptId();
        if (!deptId.equals(100l)) {
            List<SysAgent> agents = agentMapper.selectSysAgentByDeptId(deptId);
            if (CollectionUtils.isEmpty(agents)) {
                throw new ServiceException("当前用户权限异常");
            }
            Long agentId = agents.get(0).getId();

            List<SysStoreListVO> sysStores= sysStoreMapper.selectSysStoreIdAndName(agentId);
            return success(sysStores);

        }

       List<SysStoreListVO> sysStores= sysStoreMapper.selectSysStoreIdAndName(0l);
        return success(sysStores);

    }


    //商户启用禁用账号
    @PostMapping("/resetStatus")
    public AjaxResult UpOrNoAgentStatus(@RequestBody SysStoreStatusDTO statusDTO) {

        sysStoreService.UpOrNoAgentStatus(statusDTO);

        return success("修改成功");

    }
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {

        return toAjax(sysStoreService.deleteSysStoreById(id));
    }

    @GetMapping("/getStoreAddress")
    public AjaxResult getStoreAddress() {

       String address = agentMapper.getStoreAddress();

       return success(new StringBuilder(address));
    }

}
